<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head th:include="/common/inc::header"/>
<body>
<form class="layui-form">
    <input name="id" id="id" type="hidden"/>
    <br/>
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-inline">
            <input type="text" name="userName" id="userName" placeholder="请输入用户名" autocomplete="off"
                   class="layui-input" lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">账号</label>
        <div class="layui-input-inline">
            <input type="text" name="account" id="account" placeholder="请输入手机号码" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码框</label>
        <div class="layui-input-inline">
            <input type="password" name="password" id="password" lay-verify="required" placeholder="请输入密码"
                   autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">确认密码框</label>
        <div class="layui-input-inline">
            <input type="password" name="password2" id="password2" lay-verify="required" placeholder="再次确认密码"
                   autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <button class="layui-btn" id="formSubmit" lay-submit="" lay-filter="formSubmit" style="display: none">立即提交</button>
</form>
<script th:replace="/common/inc::incJs"></script>
<script>
    var form = layui.form;
    var successCallback;
    var id = getQueryString("id");
    $("#id").val(id);

    //监听提交
    form.on('submit(formSubmit)', function (data) {
        if (id != "") {
            if (data.field.password == data.field.password2) {
                $.post('/backend/security/user/updateUser', data.field, function (res) {
                    if (res.code < 0) {
                        layer.msg(res.message);
                    }
                    else {
                        layer.msg('操作成功');
                        successCallback && successCallback();
                    }
                });
            } else {
                layer.msg('两次密码不相同');
                return false;
            }
            return false;
        }
        else {
            if (data.field.password == data.field.password2) {
                $.post('/backend/security/user/addUser', data.field, function (res) {
                    if (res.code < 0) {
                        layer.msg(res.message);
                    }
                    else {
                        layer.msg('操作成功');
                        successCallback && successCallback();
                    }
                });
            } else {
                layer.msg('两次密码不相同');
                return false;
            }
            return false;
        }
        return false;
    });

    var formSubmit = function (callback) {
        successCallback = callback;
        $("#formSubmit").click();
    };


    $(function () {
        if (id != "") {
            //$("form").loadData('/backend/security/user/getById?id=' + id);
            $.post('/backend/security/user/getById', {id: id}, function (res) {
                if (res.code < 0) {
                    layer.msg(res.message);
                }
                else {
                    $("form").loadData(res.body);
                    $("#account").attr("disabled", "disabled");
                    $("#password2").val(res.body.password);
                }
            });
        }
    })
</script>
</body>
</html>